package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;
import com.zahb.qadx.model.LecturerModel;
import com.zahb.qadx.ui.activity.AttentionDetailActivity;
import com.zahb.qadx.ui.view.CircleImageView;
import com.zahb.qadx.util.ImageLoaderKt;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by： huwei.
 * Data： 2020-10-22.
 * Time： 16:42.
 * Desc：讲师adapter
 */
public class LecturerAdapter extends RecyclerView.Adapter<LecturerAdapter.LecturerViewHodler> {
    private Context mContext;
    private List<LecturerModel.LecturerData.LecturerList> lecturerLists;
    List<String> cateName = new ArrayList<>();
    public LecturerAdapter(Context mContext, List<LecturerModel.LecturerData.LecturerList> lecturerLists) {
        this.mContext = mContext;
        this.lecturerLists = lecturerLists;
    }
    public void setList(List<LecturerModel.LecturerData.LecturerList> lecturerLists) {
        this.lecturerLists = lecturerLists;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public LecturerViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LecturerViewHodler(LayoutInflater.from(mContext).inflate(R.layout.item_liecurer, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LecturerViewHodler holder, int position) {
        holder.tv_name.setText(lecturerLists.get(position).getName());
        holder.tv_desc.setText(lecturerLists.get(position).getTitle());
        String headPhoto = lecturerLists.get(position).getHeadPhoto();
        ImageLoaderKt.loadImageCircle(holder.iv_icon,headPhoto);
        cateName = lecturerLists.get(position).getCateName();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext,AttentionDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("img",lecturerLists.get(position).getHeadPhoto());
                bundle.putString("desc",lecturerLists.get(position).getTitle());//简介
                bundle.putString("type",String.valueOf(lecturerLists.get(position).getType()));
                if(cateName !=null){
                    bundle.putString("cateName",cateName.toString());//擅长
                }
                bundle.putString("certificates",lecturerLists.get(position).getCertificates());//资质
                bundle.putString("relationId",String.valueOf(lecturerLists.get(position).getId()));
                bundle.putString("name",lecturerLists.get(position).getName());
                bundle.putString("title",lecturerLists.get(position).getTitle());
                bundle.putInt("id",lecturerLists.get(position).getId());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lecturerLists !=null ? lecturerLists.size() : 0;
    }

    static class LecturerViewHodler extends RecyclerView.ViewHolder {
        private CircleImageView iv_icon;
        private AppCompatTextView tv_name, tv_desc;

        public LecturerViewHodler(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_desc = itemView.findViewById(R.id.tv_desc);
        }
    }
}
